"""
实现斐波那契函数 for  while
如果遇到可以转换为 int 类型则转换，否则抛出自定义异常 ParamsTypeError
"""


class ParamsTypeError(Exception):
    def __init__(self, ErrorInfo):
        super().__init__(self)  # 初始化父类
        self.errorinfo = ErrorInfo

    def __str__(self):
        return self.errorinfo


def fib_loop_for(n):
    a, b = 0, 1
    res = []
    try:
        n = int(n)
        for _ in range(n):
            a, b = b, a + b
            res.append(a)
        return res
    except:
        raise ParamsTypeError('n must be int')


def fib_loop_while(n):
    a, b = 0, 1
    res = []
    try:
        n = int(n)
        while n > 0:
            a, b = b, a + b
            n -= 1
            res.append(a)
        return res
    except:
        raise ParamsTypeError('n must be int')


if __name__ == '__main__':
    # for i in range(10):
    #     print(fib_loop_for(i), end=' ')
    # print("\n")
    #
    # for i in range(10):
    #     print(fib_loop_while(i), end=' ')
    print(fib_loop_for(10))
    print(fib_loop_while(10))
